package com.witmore.skyline.web.service.third.yjx.client;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.witmore.skyline.web.service.third.constants.ApiClientCommonConstant;
import com.witmore.skyline.web.service.third.yjx.dto.CloudmDeviceDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static java.lang.String.format;

/**
 * Description: ApiClient 第三方接口调用-设备相关接口
 * Author: 豆子高
 * Date: 2025/2/24 18:26
 */
@Slf4j
@Service
public class ApiDeviceClient {
    /**
     * 拉取数据
     */
    public List<CloudmDeviceDTO> fetchDeviceList(int page, int size) {
        String urlString = format(ApiClientCommonConstant.API_PREFIX + ApiClientCommonConstant.GET_DEVICE_LIST_PATH,
                TokenCache.getToken(), page, size);
        String responseResult = ApiClient.responseHandle(urlString, "获取设备列表接口");
        if (ObjectUtils.isEmpty(responseResult)) {
            return null;
        }
        return JSONArray.parseArray(responseResult, CloudmDeviceDTO.class);
    }

    /**
     * 获取设备详情
     */
    public CloudmDeviceDTO fetchDeviceSingle(String deviceSn) {
        String urlString = format(ApiClientCommonConstant.API_PREFIX + ApiClientCommonConstant.GET_DEVICE_DETAIL_PATH,
                TokenCache.getToken(), deviceSn);
        String responseResult = ApiClient.responseHandle(urlString, "获取设备详情接口");
        if (ObjectUtils.isEmpty(responseResult)) {
            log.warn("api 获取设备详情接口调用结果为空");
            return null;
        }
        return JSONObject.parseObject(responseResult, CloudmDeviceDTO.class);
    }

}
